package com.wh.service.storehousemanage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.api.Api;
import com.wh.mapper.initial.LocationMapper;
import com.wh.mapper.initial.ProductMapper;
import com.wh.mapper.stock.StockMapper;
import com.wh.mapper.storehousemanage.RemoveMapper;
import com.wh.pojo.Location;
import com.wh.pojo.Product;
import com.wh.pojo.Remove;
import com.wh.pojo.Stock;
import com.wh.vo.PageResult;
import net.sf.jsqlparser.statement.select.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class RemoveServiceImpl implements RemoveService{

    @Autowired
    private RemoveMapper removeMapper;

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private LocationMapper locationMapper;

    @Autowired
    private ProductMapper productMapper;

    //获取可用的库存
    @Override
    public Map<String, Object> getQuery(Remove remove) {

        boolean barCodeFlag = StringUtils.hasLength(remove.getBarCode());
        boolean fromLocationIdFlag = StringUtils.hasLength(remove.getFromLocationId());

        //获取传入条件下的商品
        QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>();
        stockQueryWrapper.eq(barCodeFlag,"bar_code",remove.getBarCode())
                .eq(fromLocationIdFlag,"location_id",remove.getFromLocationId());
        List<Stock> stocks = stockMapper.selectList(stockQueryWrapper);



        Set<String> barCodeList = new HashSet<>();
        Set<String> fromLocationIdList = new HashSet<>();
        Integer availableQuantity = 0;

        Map<String,Object> map = new HashMap<>();




        for (Stock stock : stocks){
            barCodeList.add(stock.getBarCode());
            fromLocationIdList.add(stock.getLocationId());
        }

        if (stocks.size() == 1){
            availableQuantity = stocks.get(0).getAvailableQuantity();
        }

        map.put("barCodeList",barCodeList);
        map.put("fromLocationIdList",fromLocationIdList);
        map.put("availableQuantity",availableQuantity);


        return map;
    }

    @Override
    public void addRemove(Remove remove) {
        //设置商品名
        Product product = productMapper.selectById(remove.getBarCode());
        remove.setProductName(product.getName());

        //设置创建时间
        remove.setCreateTime(new Api().getTime());

        //修改库存
        Stock fromStock = stockMapper.selectById(remove.getFromLocationId());
        fromStock.setLockedQuantity(fromStock.getLockedQuantity()+remove.getCount())
                .setAvailableQuantity(fromStock.getAvailableQuantity() - remove.getCount());
        stockMapper.updateById(fromStock);


        //添加记录
        removeMapper.insert(remove);

    }

    @Override
    public PageResult getRemoveList(PageResult pageResult) {
        Page<Remove> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Remove> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag,"remove_id",pageResult.getQuery());
        page = removeMapper.selectPage(page,queryWrapper);
        pageResult.setTotal((long) page.getRecords().size()).setRows(page.getRecords());
        return pageResult;
    }

    @Override
    public void updataRemove(Remove remove) {
        if (remove.getStatus().equals("已审核")) {
            remove.setCheckTime(new Api().getTime());
        }else if (remove.getStatus().equals("待审核")){
            remove.setCheckTime("").setCheckPerson("");
        }
        removeMapper.updateById(remove);
    }
}
